package com.polaris.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout
import com.bumptech.glide.Glide
import com.polaris.live.databinding.WidgetRoomFramelBinding
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.bubble.DownLoadFileUtil
import com.polaris.live.widget.high_light.isAttachToWindow
import org.libpag.PAGFile

/**
 * RoomFrameView房間框
 *
 * @author Created by 天晴 on 2024/12/19 10:41
 */
class RoomFrameView(context: Context, attributeSet: AttributeSet) :
    RelativeLayout(context, attributeSet) {

    private val mBinding = WidgetRoomFramelBinding.inflate(LayoutInflater.from(context), this)

    private var lastPath: String? = null //当前在显示的框

    private var tempPath: String? = null //临时

    init {
        visibility = GONE
    }

    private fun isPag(frame: String?): Boolean {
        return frame != null && frame.contains(".pag?")
    }

    /**
     * 设置房間框
     */
    fun setRoomFrame(frame: String?) {
        if (frame != null) {
            visibility = VISIBLE
            if (isPag(frame)) {
                if (lastPath != frame) {
                    mBinding.framePag.visibility = View.GONE
                }
                mBinding.frameImg.visibility = View.GONE
                if (!mBinding.framePag.isPlaying || lastPath != frame) {
                    downPag(frame)
                }
            } else {
                lastPath = frame
                if (mBinding.framePag.isPlaying) {
                    mBinding.framePag.pause()
                }
                mBinding.frameImg.visibility = View.VISIBLE
                mBinding.framePag.visibility = View.GONE
                Glide.with(mBinding.frameImg)
                    .load(ImageUtils.imgPath(frame))
                    .into(mBinding.frameImg)
            }
        } else {
            lastPath = null
            if (mBinding.framePag.isPlaying) {
                mBinding.framePag.pause()
            }
            mBinding.framePag.visibility = View.GONE
            mBinding.frameImg.visibility = View.GONE
        }

    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        tempPath?.let {
            setRoomFrame(it)
            tempPath = null
        }
    }

    private fun downPag(frame: String) {
        DownLoadFileUtil.loadPag(frame) { nPath, realPath ->
            if (frame == nPath) {
                if (isAttachToWindow()) {
                    lastPath = frame
                    val pageFile = PAGFile.Load(realPath) ?: return@loadPag
                    mBinding.framePag.apply {
                        setRepeatCount(-1)
                        composition = pageFile
                        play()
                        mBinding.framePag.visibility = View.VISIBLE
                    }
                } else {
                    tempPath = frame
                }

            }
        }
    }
}